/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package emailing;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Kate
 */
public class Emailer {

    String protocol;
    String host;
    String user;
    String pass;
    int port;
    Properties props;
    Session session;


    /**
     *
     * Initiates object with following values: 
     * mail server= smtp.gmail.com
     * connects to mail server using account deltaspringproject@gmail.com
     * 
     */
    
    public Emailer() {
        user = "deltaspringproject@gmail.com";
        pass = "delta2013";
        protocol = "smtp";
        port = 587;
        host = "smtp.gmail.com";
         // Get system properties
        props = System.getProperties();
        //starttls is required by Google
        props.put("mail.smtp.starttls.enable", "true");
        session = Session.getDefaultInstance(props);
        

    }

     /**
     * Sends e-mail. 
     *
     * @param     to  recipient e-mail address
     * @param     subject  e-mail subject
     * @param     body e-mail body
     */

    
    public void sendEmail(String to, String subject, String body) {
         try {
            // Create a MimeMessage object. 
            MimeMessage message = new MimeMessage(session);

            // Set To
            message.addRecipients(Message.RecipientType.TO, to);

            // Set Subject
            message.setSubject(subject);

            // Set the actual message
            message.setText(body);
            sendEmail(message);
            
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
    
    
     /**
     * Sends e-mail. 
     *
     * @param     message message to be sent
     */
    public void sendEmail(MimeMessage message){
         try {
            // Send message
            Transport transport = session.getTransport(protocol);
            transport.connect(host, port, user, pass);
           
            transport.sendMessage(message,message.getAllRecipients());
            transport.close();
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
         
    
        
    }
}
